#!/bin/sh

#
# This validates VT100, 16-color, 256-color and true color attributes
# against tmux.  It is not run directly, but as subtests based on the
# executable's name for vt100, 16color, 256color8, 256color248 and
# truecolor.
# This is because Mosh internally represents the first 8 values of the
# 256color space as though they were the 16-color values they are
# equivalent to.  tmux does not filter this out on its redisplay, so
# compares on these values fail though they are visually identical.
#

# shellcheck source=e2e-test-subrs
. "$(dirname "$0")/e2e-test-subrs"
PATH=$PATH:.:$srcdir
# Need 2.3 for true-color support (2.2 may work also)
if [ "$(basename "$0")" = emulation-attributes-truecolor.test ] &&
   ! tmux_check 2 3; then
    printf "tmux does not support true color\n" >&2
    exit 77
fi
# Need 2.4 for BCE support
if [ "$(basename "$0")" = emulation-attributes-bce.test ] &&
   ! tmux_check 2 4; then
    printf "tmux does not support BCE\n" >&2
    exit 77
fi

# tmux 3.3a has a behavior change in how BCE is handled, which has
# been reverted: https://github.com/tmux/tmux/issues/3339
if [ "$(basename "$0")" = emulation-attributes-bce.test ] &&
   tmux_check 3 3a && ! tmux_check 3 3b; then
    printf "tmux 3.3a has incompatible BCE behavior\n" >&2
    exit 77
fi

# Top-level wrapper.
if [ $# -eq 0 ]; then
    e2e-test "$0" baseline direct verify
    exit
fi

# OK, we have arguments, we're one of the test hooks.
if [ $# -ne 1 ]; then
    fail "bad arguments %s\n" "$@"
fi

test_true_color()
{
    s=$(printf "\033[0")
    for i in $@; do
        s="$s;$i"
    done
    s="${s}m"

    for attr in $(seq 0 76); do
        r=$((255-(attr*255/76)))
        g=$((attr*510/76))
        b=$((attr*255/76))
        if [ $g -gt 255 ]; then
            g=$((510-g))
        fi
        invr=$((255-r))
        invg=$((255-g))
        invb=$((255-b))
        c="E"

        printf "%s" "$s"
        printf "\033[48;2;%d;%d;%dm" $r $g $b;
        printf "\033[38;2;%d;%d;%dm" $invr $invg $invb
        printf "%s\033[m" "$c"
    done
    printf "\n"
}

baseline()
{
    # Strip our name to the last dash-separated word before the .test suffix.
    testname=$(basename "$1")
    testname=${testname%%.test}
    testname=${testname##*-}

    printf '\033[H\033[J'


    case $testname in
	# Traditional ancient VT100 attributes.
	vt100)
	    for attr in 0 1 4 5 7; do
		printf '\033[%dmE\033[m ' $attr
	    done
	    ;;
	# 16-color attributes.
	16color)
	    for attr in $(seq 30 37) $(seq 39 47) 49; do
		printf '\033[%dmE\033[m ' "$attr"
	    done
	    ;;
	# First 8 256-color attributes.  Comparing mosh and tmux fails.
	256color8)
	    for attr in $(seq 0 7); do
		printf '\033[38;5;%dmE\033[m ' "$attr"
		printf '\033[48;5;%dmM\033[m ' "$attr"
	    done
	    ;;
	# Last 248 256-color attributes.
	256color248)
	    for attr in $(seq 8 255); do
		printf '\033[38;5;%dmE\033[m ' "$attr"
		printf '\033[48;5;%dmM\033[m ' "$attr"
	    done
	    ;;
        # True color.
        # See https://gist.github.com/XVilka/8346728 for the test case
        truecolor)
            echo "Normal:"
            test_true_color
            echo "Bold:"
            test_true_color 1
            echo "Italic:"
            test_true_color 3
            echo "Underline:"
            test_true_color 4
            echo "Blink:"
            test_true_color 5
            echo "Inverse:"
            test_true_color 7
            echo "Invisible:"
            test_true_color 8
            echo "Bold, italic and underline:"
            test_true_color 1 3 4
            ;;
	# BCE in combination with various color modes.
	bce)
	    # True color.
	    printf '\033[48;2;255;0;255m\033[H\033[JTrue color\n'
	    printf '\033[48;5;32m\033[J256 color\n'
	    printf '\033[42m\033[J16 color\n'
	    printf '\033[0mdone\n'
	    ;;
	*)
	    fail "unknown test name %s\n" "$1"
	    ;;
    esac

    printf '\033[mend\n'
} 

case $1 in
    baseline|direct)
	baseline "$0";;
    *)
	fail "unknown test argument %s\n" "$1";;
esac
